package com.yuantu.judiciary.security.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.yuantu.judiciary.dao.IDiscountCodeDao;
import com.yuantu.judiciary.model.domain.DiscountCodeDO;
import com.yuantu.judiciary.model.vo.DiscountCodeImportVO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.StringJoiner;

@Data
@Component
public class DiscountCodeExcelImportVerifyHandler implements IExcelVerifyHandler<DiscountCodeImportVO> {


    @Autowired
    private IDiscountCodeDao discountCodeDao;


    @Override
    public ExcelVerifyHandlerResult verifyHandler(DiscountCodeImportVO discountCodeImport) {

        StringJoiner joiner = new StringJoiner(",");
        String discountCode = discountCodeImport.getDiscountCode();
        if (StringUtils.isBlank(discountCode)) {
            joiner.add("推荐码不能为空");
        } else {
            DiscountCodeDO existDiscountCode = discountCodeDao.findDiscountCodeByDiscountCodeAndDeletedFalse(discountCode);
            if (existDiscountCode != null) {
                joiner.add("数据库中推荐码已存在");
            }
        }
        if (joiner.length() != 0) {
            return new ExcelVerifyHandlerResult(false, joiner.toString());
        }
        return new ExcelVerifyHandlerResult(true);
    }
}
